package map.viewmodel

import android.view.MotionEvent
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.baidu.mapapi.map.BaiduMap
import com.baidu.mapapi.map.MapPoi
import com.baidu.mapapi.map.MyLocationConfiguration
import com.baidu.mapapi.model.LatLng
import com.baidu.mapapi.search.core.PoiInfo
import com.baidu.mapapi.search.poi.PoiDetailResult
import com.baidu.mapapi.search.poi.PoiDetailSearchResult
import com.zjw.zy.ext.launchInIo
import com.zjw.zy.ext.launchInMain
import com.zjw.zy.smi.JustWithIt
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import map.constant.MapMode
import map.constant.MapMode.MapModeDes
import map.default.DefaultGetPoiSearchResultListener
import map.ext.*

class MapViewModel : MapBaseViewModel() {

    private var followJob: Job? = null

    //地图模式
    @MapModeDes
    private val mMapModeLive: MutableLiveData<Int> = MutableLiveData(MapMode.NORMAL_MAP)
    fun getMapMode(): LiveData<Int> = mMapModeLive

    //选中的搜索poi
    private val mPoiDetail: MutableLiveData<PoiDetailResult?> = MutableLiveData()
    fun getPoiDetails(): LiveData<PoiDetailResult?> = mPoiDetail

    //选中的搜索poi
    private val mMapTouch: MutableLiveData<MotionEvent> = MutableLiveData()
    fun getMapTouch(): LiveData<MotionEvent> = mMapTouch


    private val mMapTouchListener = BaiduMap.OnMapTouchListener {
        locationManager().locationMode(MyLocationConfiguration.LocationMode.NORMAL)

        //传入null，隐藏详情页
        mPoiDetail.postValue(null)
        mMapTouch.postValue(it)

        followJob?.cancel()
        followJob = launchInMain {
            //十秒后自车
            delay(10_000L)
            toSelfLocation()
        }
    }

    private val mMapClickListener = object : BaiduMap.OnMapClickListener {
        override fun onMapClick(p0: LatLng?) {
        }

        override fun onMapPoiClick(p0: MapPoi?) {
            searchManager().let { searchManager ->
                launchInIo {
                    p0?.let { poi ->
                        searchManager.poiDetailSearch(poi.uid,
                            object : DefaultGetPoiSearchResultListener {
                                override fun onGetPoiDetailResult(p0: PoiDetailResult?) {
                                    p0?.let {
                                        mPoiDetail.postValue(p0)
                                    }
                                }
                            })
                    }
                }
            }
        }
    }

    private val mMapPoiClickListener = JustWithIt<PoiInfo> {
        //深度搜索
        launchInIo {
            searchManager().poiDetailSearch(it.uid, object : DefaultGetPoiSearchResultListener {
                override fun onGetPoiDetailResult(p0: PoiDetailSearchResult?) {
//                    p0?.let {
//                        mPoiDetail.postValue(p0)
//                    }
                }

                override fun onGetPoiDetailResult(p0: PoiDetailResult?) {
                    p0?.let {
                        mPoiDetail.postValue(p0)
                    }
                }
            })
        }
    }

    override fun onPageEnter() {
        super.onPageEnter()

        toSelfLocation()

        mapManager().addMapOnTouchListener(mMapTouchListener)
        mapManager().addMapOnClickListener(mMapClickListener)
        mapManager().addPoiOverLayClickListener(mMapPoiClickListener)
        mapManager().showZoomButton()
    }

    override fun onPageExit() {
        super.onPageExit()
        followJob?.cancel()
        mapManager().removeMapOnTouchListener(mMapTouchListener)
        mapManager().removeMapOnClickListener(mMapClickListener)
        mapManager().removePoiOverLayClickListener(mMapPoiClickListener)

    }

    fun toSelfLocation(toSelf: Boolean = true) {
        if (toSelf) {
            locationManager().locationMode(MyLocationConfiguration.LocationMode.FOLLOWING)
            mapManager().zoomBy(16f)
        } else {
            followJob?.cancel()
            locationManager().locationMode(MyLocationConfiguration.LocationMode.NORMAL)
        }
    }

    fun switchMapMode() {
        val currentMode = mMapModeLive.value

        val nextMode = if (currentMode == MapMode.NORMAL_MAP) {
            MapMode.SATELLITE_MAP
        } else {
            MapMode.NORMAL_MAP
        }
        mMapModeLive.postValue(
            nextMode
        )
        mapManager().switchMapMode(nextMode)
    }

    fun hideZoom() {
        mapView().showZoomControls(false)
    }

    fun showZoom() {
        mapView().showZoomControls(true)
    }
}